﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.IO;
using System.Threading;
using System.Collections.ObjectModel;

namespace XMM.Dialoges.Library
{
    /// <summary>
    /// Interaktionslogik für EditDiskMedia.xaml
    /// </summary>
    public partial class EditDiskMedia : Window
    {
        public Objects.DiskMedia SelectedDiskMedia { get; set; }

        public ObservableCollection<Objects.DiskMedia> AvailableMedia
        {
            get
            {
                return _AvailableMedia;
            }
        }

        ObservableCollection<Objects.DiskMedia> _AvailableMedia;

        Controls.Library.Holster_DiskMedia ParentDiskMediaHolster { get; set; }

        public EditDiskMedia(Controls.Library.Holster_DiskMedia ctlParentDiskMediaHolster)
        {
            InitializeComponent();

            ParentDiskMediaHolster = ctlParentDiskMediaHolster;
        }

        public void resetGUI()
        {
            if (SelectedDiskMedia != null && SelectedDiskMedia.DiskMediaID != 0)
            {
                lvAvailableMedia.SelectedItem = SelectedDiskMedia;
                lblSelectedDiskMedia.Content = SelectedDiskMedia.MediaName;
                rdbDiskMedia.IsChecked = true;
                rdbSeperateMedia.IsChecked = false;
            }
            else
            {
                rdbDiskMedia.IsChecked = false;
                rdbSeperateMedia.IsChecked = true;
            }
        }

        public void reloadMedia()
        {
            _AvailableMedia = new ObservableCollection<Objects.DiskMedia>();
            
            List<Objects.DiskMedia> lAvailableMedia = Objects.DiskMedia.getAllDiskMedia();
            if (lAvailableMedia != null && lAvailableMedia.Count > 0)
            {
                foreach (Objects.DiskMedia oDisk in lAvailableMedia)
                {
                    _AvailableMedia.Add(oDisk);
                }
            }


            lvAvailableMedia.ItemsSource = AvailableMedia;
            lvAvailableMedia.ItemsPanel.LoadContent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            reloadMedia();
            resetGUI();
        }

        private void lvAvailableMedia_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (lvAvailableMedia.SelectedIndex != -1)
            {
                Objects.DiskMedia oDisk = (Objects.DiskMedia)lvAvailableMedia.SelectedItem;
                lblSelectedDiskMedia.Content = oDisk.MediaName;
                SelectedDiskMedia = oDisk;
                rdbDiskMedia.IsChecked = true;
            }
            else
            {
                lblSelectedDiskMedia.Content = "-";
                SelectedDiskMedia = null;
            }
        }

        private void rdbSeperateMedia_Checked(object sender, RoutedEventArgs e)
        {
            if (lblSelectedDiskMedia != null)
            {
                lblSelectedDiskMedia.Content = "-";
            }
            SelectedDiskMedia = null;
        }

        private void tsbAddNewMedia_Click(object sender, RoutedEventArgs e)
        {
            Dialoges.Library.AddDiskMedia dlgAddDiskMedia = new AddDiskMedia(this);
            dlgAddDiskMedia.ShowDialog();
        }

        private void tsbCancel_Click(object sender, RoutedEventArgs e)
        {
            Close();
        }

        private void tsbSave_Click(object sender, RoutedEventArgs e)
        {
            if ((bool)rdbSeperateMedia.IsChecked)
            {
                ParentDiskMediaHolster.SelectedDisk = null;
            }
            else
            {
                if (SelectedDiskMedia != null && SelectedDiskMedia.DiskMediaID != 0)
                {
                    ParentDiskMediaHolster.setDiskMedia(SelectedDiskMedia);
                }
                else
                {
                    ParentDiskMediaHolster = null;
                }
            }

            ParentDiskMediaHolster.setStatusIcon();

            Close();
        }

        private void tsbRemoveSelectedMedia_Click(object sender, RoutedEventArgs e)
        {
            if (lvAvailableMedia.SelectedIndex != -1)
            {
                if (MessageBox.Show("Der ausgewählte Sammel-Datenträger wird aus der Mediathek entfernt! Sind Sie sicher?", "Sammel-Datenträger löschen", MessageBoxButton.YesNo, MessageBoxImage.Warning) == MessageBoxResult.Yes)
                {
                    Objects.DiskMedia oSelectedDisk = (Objects.DiskMedia)lvAvailableMedia.SelectedItem;
                    if (oSelectedDisk.AquiredMedia == 0)
                    {
                        if (oSelectedDisk.Delete())
                        {
                            _AvailableMedia.RemoveAt(lvAvailableMedia.SelectedIndex);
                            lvAvailableMedia.ItemsSource = AvailableMedia;
                            lvAvailableMedia.ItemsPanel.LoadContent();
                        }
                    }
                    else
                    {
                        MessageBox.Show("Der ausgewählte Sammel-Datenträger wird für " + oSelectedDisk.AquiredMedia + "Medien in der Datenbank verwendet und kann nicht gelöscht werden!", "Sammel-Datenträger in Verwendung", MessageBoxButton.OK, MessageBoxImage.Exclamation);
                    }
                }
            }
        }

        private void lvAvailableMedia_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            if (lvAvailableMedia.SelectedIndex > -1)
            {
                SelectedDiskMedia = (Objects.DiskMedia)lvAvailableMedia.SelectedItem;
                ParentDiskMediaHolster.setDiskMedia(SelectedDiskMedia);
                ParentDiskMediaHolster.setStatusIcon();
                Close();
            }
        }
    }
}
